﻿using Orther.Model.param;
using Orther.Service.Miyao;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using VOL.Core.ManageUser;

namespace Orther.BLL.Miyao
{
    public class PeijianBLL
    {
        private PeijianService peijianService = new PeijianService();
        public TData<List<qx_peijian>> GetPageList(PeijianParam param, Pagination pagination)
        {
            TData<List<qx_peijian>> obj = new TData<List<qx_peijian>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = peijianService.GetPageList(param, pagination);
            obj.Tag = 1;
            obj.Total = pagination.TotalCount;
            return obj;
        }

        public async Task<TData<List<qx_peijian>>> GetList(PeijianParam param, int? topNum)
        {
            TData<List<qx_peijian>> obj = new TData<List<qx_peijian>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = await peijianService.GetList(param, topNum);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<qx_peijian>> GetObject(PeijianParam param)
        {
            TData<qx_peijian> obj = new TData<qx_peijian>();
            obj.Data = await peijianService.GetObject(param);
            obj.Tag = 1;
            return obj;
        }


        public TData SaveForm(qx_peijian entity)
        {
            TData obj = new TData();
            if (entity == null)
            {
                obj.Message = "qx_peijian对象为空！";
                return obj;
            }

            if (entity.pname == null)
            {
                obj.Message = "操作失败，商品名称不能为空！";
                return obj;
            }

            //新增操作
            if (entity.Id == Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                //初始化主键值
                entity.Id = Guid.NewGuid();
                entity.IsDeleted = 0;
                //初始化字段的默认值
                List<object[]> defaultCols = new List<object[]>();
                defaultCols.Add(new object[] { "CreateDate", DateTime.Now });
                defaultCols.Add(new object[] { "CreateID", UserContext.Current.UserId });
                defaultCols.Add(new object[] { "Creator", UserContext.Current.UserName });
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + defaultCols[2][1] + "添加了一条配件资料的信息，配件名称为:【" + entity.pname + "】。";
                oLog.Category = "配件档案添加";
                oLog.MenuModel = "配件资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                peijianService.SaveForm(entity, oLog, defaultCols);
            }
            //更新操作
            else
            {
                List<object[]> defaultCols = new List<object[]>();
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "更新了一条配件资料的信息，" +
                    "配件名称变更为:【" + entity.pname + "】。" +
                    "车型设置为:【" + entity.chexing + "】" +
                    "销售价设置为:【" + entity.xiaoshoujia + "】";
                oLog.Category = "配件档案修改";
                oLog.MenuModel = "配件资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                peijianService.UpdateForm(entity, oLog, defaultCols);
            }


            obj.Tag = 1;

            return obj;
        }


        public async Task<TData> deleteFrom(Guid id)
        {
            TData obj = new TData();
            //第一步：获取要删除的企业名称
            PeijianParam param = new PeijianParam();
            param.Id = id;
            qx_peijian peijian = await peijianService.GetObject(param);
            peijian.IsDeleted = 1;
            //第二步：日志信息操作
            operatelog oLog = new operatelog();
            oLog.Log_Id = Guid.NewGuid();
            oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "删除了配件名称为：【" + peijian.pname + "】的信息。";
            oLog.Category = "配件资料删除";
            oLog.MenuModel = "配件资料";
            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            peijianService.UpdateForm(peijian, oLog);
            obj.Tag = 1;
            return obj;
        }


    }
}
